﻿$.URL_UPLOAD = "upload.aspx";
$(document).ready(function () {
    $("input[type='upload']").each(function (i, e) {
        var showProgress = $(this).data("progress");

        var options = {
            extensions: $(this).data("type"),
            maxSize: $(this).data("max-size"),
            autoSubmit: $(this).data("auto-submit")
        };
        var element = $("<iframe>").attr({ scrolling: "no", frameborder: "0", src: $.URL_UPLOAD, id: "ifrm_upload_" + i + "_" }).css({ width: $(this).width() + "px", height: $(this).height() + "px" }).insertAfter($(this));
        element.data("editor", this);

        var progress_bar = $("<div id=\"div_progress_" + i + "_\"><div style=\"display:none;\" id=\"loading_div\">正在上传，请稍等...</div><div id=\"progress_div\"><div>进度: {progress} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"javascript:void(0);\" onclick=\"reloadUpload(" + i + ",'" + $.URL_UPLOAD + "&rnd=' + (new Date().getTime()));\">取消</a></div><div>速度: {speed}</div><div>耗时: {time}</div></div></div>").css({
            "font-family": "微软雅黑",
            "font-size": "12px",
            "border": "1px solid #007712",
            "background-color": "#b3ffbe",
            "width": " 200px",
            "position": "absolute",
            "display": "none"
        }).insertAfter(element);

        window.reloadUpload = function (idx, src) {
            $("#div_progress_" + idx + "_").css("display", "none");
            $("#ifrm_upload_" + idx + "_").attr("src", src);
        };

        element.on("load", function (e) {
            options.width = $(this).width();
            options.height = $(this).height();
            options.loadingPanel = this.id.replace("ifrm_upload_", "#div_progress_");
            options.beforeUpload = window.beforeUpload;
            if (showProgress) options.showUploadProgress = window.showUploadProgress;
            options.uploadCompleted = window.uploadCompleted;
            this.contentWindow.changeOptions(options);
        });

        $(this).hide();
    });

    $("form").on("submit", function () {
        var uploaders = $(this).find(".control-upload,[data-required='true']");
        var isUploaded = true;
        uploaders.each(function (i, e) {
            if (!e.value) {
                if (!$(e).data("error")) {
                    alert($(e).data("error-msg"));
                    $(e).focus();
                }
                else {
                    $($(e).data("error")).text($(e).data("error-msg"));
                    $(e).focus();
                }
                return isUploaded = false;
            }
        });
        return isUploaded;
    });
});

window.beforeUpload = function (cid, form) {
    var progress = $(cid);

    progress.children("#loading_div").css("display", "inherit");
    progress.children("#progress_div").css("display", "none");
    progress.css({
        top: ($(progress[0].uploadControl).height() + 1) + "px",
        left: "0px",
    });
}

window.showUploadProgress = function (cid, uid, src) {
    var progress = $(cid);

    var interval = setInterval(function () {
        $.ajax({
            type: "POST",
            async: true,
            dataType: "json",
            url: src,
            data: { uid: uid, progress: "get" },
            success: function (result) {
                if (result.success === true) {
                    progress.children("#loading_div").css("display", "none;");
                    progress.children("#progress_div").css("display", "inherit");

                    if (result.data.done) {
                        progress.hide();
                        clearInterval(interval);
                    }
                    else {
                        for (var p in result.data) {
                            if (progress.html().indexOf("{" + p + "}") < 0) continue;
                            progress.html(progress.html().replace("{" + p + "}", result.data[p]));
                        }
                    }
                }
            }
        });

    }, 1000);
};

window.uploadCompleted = function (cid, form, path) {
    var el = $(cid).hide();
    $(cid.replace("#div_progress_", "#ifrm_upload_")).each(function (i, ele) {
        $($(ele).data("editor")).val(path);
    });
}